package com.pz.gulimall.ware.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.pz.gulimall.ware.vo.req.MergeReqVo;
import com.pz.gulimall.ware.vo.req.PurchaseDoneReqVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.pz.gulimall.ware.entity.PurchaseEntity;
import com.pz.gulimall.ware.service.PurchaseService;
import com.pz.gulimall.common.utils.PageUtils;
import com.pz.gulimall.common.utils.R;

import javax.validation.Valid;


/**
 * 采购信息
 *
 * @author PZ
 * @email pzs@gmail.com
 * @date 2024-02-02 11:49:35
 */
@RestController
@RequestMapping("ware/purchase")
public class PurchaseController {
    @Autowired
    private PurchaseService purchaseService;

    @PostMapping("/done")
    public R finished(@Valid @RequestBody PurchaseDoneReqVo vo){
        purchaseService.donePurchase(vo);
        return R.ok();
    }


    /**
     * 领取采购单(状态是 新建 或 已分配 )
     * @param purchaseIds  采购单id集合
     * @return
     */
    @PostMapping("/received")
    public R receivePurchase(@RequestBody List<Long> purchaseIds){

        purchaseService.received(purchaseIds);
        return R.ok();
    }


    /**
     * 合并采购需求，请求参数如下
     * {
     *   purchaseId: 1, //整单id，可为空
     *   items:[1,2,3,4] //合并项 商品id集合
     * }
     */
    @PostMapping("/merge")
    public R merge(@RequestBody MergeReqVo vo){
        purchaseService.Merge(vo);
        return R.ok();
    }

    /**
     * 获取未领取的采购单
     */
    @RequestMapping("/unreceive/list")
    public R getUnReceive(@RequestParam Map<String, Object> params){
        PageUtils page = purchaseService.queryPageUnReceive(params);

        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = purchaseService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("ware:purchase:info")
    public R info(@PathVariable("id") Long id){
		PurchaseEntity purchase = purchaseService.getById(id);

        return R.ok().put("purchase", purchase);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("ware:purchase:save")
    public R save(@RequestBody PurchaseEntity purchase){
        purchase.setCreateTime(new Date());
        purchase.setUpdateTime(new Date());
		purchaseService.save(purchase);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("ware:purchase:update")
    public R update(@RequestBody PurchaseEntity purchase){
		purchaseService.updateById(purchase);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("ware:purchase:delete")
    public R delete(@RequestBody Long[] ids){
		purchaseService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
